Lab Assignment 3
Due date : September 3, 12 PM EST
Total Marks : 60
Topics |
Lab Exercises |
Points |
Variables Constants |
|
|
Prelab Exercises
1. What is the difference between a variable and a constant?
2. Explain what each of the lines below does. Be sure to indicate how each statement is different from the others.
a. int x;
b. int x = 3;
c. x = 3;
3. The following program reads three integers and prints the average. Fill in the blanks so that it will work correctly.
// *******************************************************************
// Average.java
//
// Read three integers from the user and print their average
// *******************************************************************
import java.util.Scanner;
public class Average
{
public static void main(String[] args)
{
int val1, val2, val3;
double average;
Scanner scan = new Scanner(System.in) ;
// get three values from user
System.out.println("Please enter three integers and " +
"I will compute their average");
____________________________________
____________________________________
____________________________________
//compute the average
____________________________________
//print the average
____________________________________
}
}
4. Given the declarations below, find the result of each expression.
int a = 3, b = 10, c = 7;
double w = 12.9, y = 3.2;
a. a + b * c
b. a - b - c
c. a / b
d. b / a
e. a - b / c
f. w / y
g. y / w
h. a + w / b
i. a % b / y
j. b % a
k. w % y
5. Run the following program. You will find a lot of syntax errors. List them and correct them one by one until the program is ready to run. Run the program and make sure that it compiles without any error.
// File: Errors.java
// Purpose: A program with lots of syntax errors
// Correct all of the errors (STUDY the program carefully!!)
#import java.util.Scanner;
public class Errors
{
public static void main (String[] args)
{
String Name; / Name of the user
int number;
int numSq;
Scanner scan = new Scanner(System.in);
System.out.print ("Enter your name, please: ")
Name = scan.nextInt();
System.out.print ("What is your favorite number?);
number = scan.nextInt();
numSq = number * number;
System.out.println (Name ", the square of your number is "
numSquared);
}
6. Trace the execution of the following program assuming the input stream contains the numbers 10, 3, and 14.3. This means that you have to find out the values of each variable at each line only by reading the program and without running it. Fill up the table that shows the value of each variable at each line. Also show the output (exactly as it would be printed).
// FILE: Trace.java
// PURPOSE: An exercise in tracing a program and understanding
// assignment statements and expressions.
import java.util.Scanner;
public class Trace
{
public static void main (String[] args)
{
int one, two, three;
double what;
Scanner scan = new Scanner(System.in);
System.out.print ("Enter two integers: ");
one = scan.nextInt();
two = scan.nextInt();
System.out.print("Enter a floating point number: ");
what = scan.nextDouble();
three = 4 * one + 5 * two;
two = 2 * one;
System.out.println ("one " + two + ":" + three);
one = 46 / 5 * 2 + 19 % 4;
three = one + two;
what = (what + 2.5) / 2;
System.out.println (what + " is what!");
}
}
Type |
Variable |
Value at line 1 |
Value at line 2 |
Value at line 5 |
Value at line 6 |
Value at line 8 |
Value at line 9 |
Value at line 10 |
Value at line 12 |
Value at line 13 |
Value at line 14 |
int |
one |
|
|
|
|
|
|
|
|
|
|
two |
|
|
|
|
|
|
|
|
|
|
|
three |
|
|
|
|
|
|
|
|
|
|
|
double |
what |
|
|
|
|
|
|
|
|
|
|
Submit the following files: